package com.apply.service;

import com.apply.entity.ExamInfo;
import com.apply.repository.ExamRepository;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * @author tianp
 */
@Service
public class ExamService {
    @Autowired
    private ExamRepository examRepository;
    private final int pageSize = 10;

    @Transactional(rollbackFor = Exception.class)
    public void save(ExamInfo examInfo) {
        examRepository.save(examInfo);
    }

    @Transactional(rollbackFor = Exception.class)
    public ExamInfo findOne(Long id) {
        ExamInfo examInfo = examRepository.findOne(id);
        Date date = new Date();
        if (date.getTime() < examInfo.getApplyStart().getTime()) {
            examInfo.setStatus("未开始");
        } else if (date.getTime() <= examInfo.getApplyEnd().getTime()) {
            examInfo.setStatus("正在进行");
        } else {
            examInfo.setStatus("已结束");
        }
        return examInfo;
    }

    public Page<ExamInfo> findAllByPage(String examName, Integer pageNum) {
        if (null == pageNum) {
            pageNum = 0;
        }
        Page<ExamInfo> list = null;
        if (StringUtils.isNotEmpty(examName)) {
            list = examRepository.findByExamNameLikeOrderByCreateTimeDesc("%" + examName + "%", new PageRequest(pageNum, this.pageSize));
        } else {
            Sort sort = new Sort(Sort.Direction.DESC, "createTime");
            list = examRepository.findAll(new PageRequest(pageNum, this.pageSize, sort));
        }
        Date date = new Date();
        for (ExamInfo examInfo : list.getContent()) {
            if (date.getTime() < examInfo.getApplyStart().getTime()) {
                examInfo.setStatus("未开始");
            } else if (date.getTime() <= examInfo.getApplyEnd().getTime()) {
                examInfo.setStatus("正在进行");
            } else {
                examInfo.setStatus("已结束");
            }
        }
        return list;
    }

    /**
     * @Description: 可选择的修改考试信息
     * @Param:
     * @return:
     * @Author: tianp
     * @Date: 2018/5/3
     */
    public Boolean updateBySelective(ExamInfo examInfo) {
        ExamInfo one = examRepository.findOne(examInfo.getId());
        //报名开始时间
        if (null != examInfo.getApplyStart()) {
            one.setApplyStart(examInfo.getApplyStart());
        }
        //报名结束时间
        if (null != examInfo.getApplyEnd()) {
            one.setApplyEnd(examInfo.getApplyEnd());
        }
        //考试开始时间
        if (null != examInfo.getExamStart()) {
            one.setExamStart(examInfo.getExamStart());
        }
        //考试结束时间
        if (null != examInfo.getExamEnd()) {
            one.setExamEnd(examInfo.getExamEnd());
        }
        //考试名称
        if (null != examInfo.getExamName()) {
            one.setExamName(examInfo.getExamName());
        }
        //考试地点
        if (null != examInfo.getExamLocation()) {
            one.setExamLocation(examInfo.getExamLocation());
        }
        //考试费用
        if (null != examInfo.getPayMoney()) {
            one.setPayMoney(examInfo.getPayMoney());
        }
        //考试须知
        if (null != examInfo.getExamNotice()) {
            one.setExamNotice(examInfo.getExamNotice());
        }
        ExamInfo save = examRepository.save(one);
        if (null != save) {
            return true;
        }
        return false;
    }
}
